import aioredis
import asyncio
import pymongo
from datetime import datetime
import sys
from datetime import timedelta
import json
import redis

from cmcc_10086 import cmcc_100862_query

conn = redis.Redis()
client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
db = client.cs_recharge
coll = db.rechange

# orderdate = sys.argv[1]
# d = datetime.strptime(orderdate, "%Y%m%d")
# line = datetime(2019,2,1,13,40).timestamp()
amount = 0
# for ordate in ["201901%02d" % i for i in range(1, 32)]:
test_range = coll.find({'orderdate': '20190305', 'pay_type': None,'orderid': {'$ne': None}, 'tel_type': 'cmcc', 'orderstate': '1'})
# print("话费订单号", "渠道订单号", "状态", "金额", "订单时间", "查询时间", sep=",")
# for i in test_range:
#     amount += int(i['amount'])
#
# print(amount)
loop = asyncio.get_event_loop()
redis = loop.run_until_complete(
    aioredis.create_redis(address=('localhost', 6379), db=0, loop=loop)
)
for obj in test_range:
    user = db.cmcc_username.find_one(obj['username'])
    task = asyncio.ensure_future(cmcc_100862_query(obj['order_id'], obj['order_number'], redis, user['username'], user['passwd']))
    res = loop.run_until_complete(task)
    print(obj['ordernumber'], obj['payorderno'], * res)